import { post, apiPaths } from '../../utils/request.js'

Page({
  data: {
    teamList: [],
    searchKeyword: ''
  },

  onLoad() {
    this.loadTeamList()
  },

  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    })
    this.loadTeamList()
  },

  loadTeamList() {
    // 调用获取团队列表接口
    post(apiPaths.getTeams).then(res => {
      if (res.code === 200) {
        let teams = res.data || []
        
        // 根据搜索关键词过滤
        if (this.data.searchKeyword) {
          teams = teams.filter(team => 
            team.name.includes(this.data.searchKeyword) ||
            (team.description && team.description.includes(this.data.searchKeyword))
          )
        }
        
        this.setData({
          teamList: teams
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.message || '获取团队列表失败',
        icon: 'none'
      })
    })
  },

  onTeamTap(e) {
    const teamId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/teamDetail/teamDetail?id=${teamId}`
    })
  },

  onCreateTeam() {
    wx.navigateTo({
      url: '/pages/teamDetail/teamDetail?action=create'
    })
  }
})